﻿Imports System.Text.RegularExpressions

'***********************************************************************
'Magic Chimney Imp (MCI)
'Copyright (C) 2011  David Frendin (david.frendin@gmail.com)
'
'This program is free software: you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published by
'the Free Software Foundation, either version 3 of the License, or
'(at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details.
'
'You should have received a copy of the GNU General Public License
'along with this program.  If not, see <http://www.gnu.org/licenses/>.
'***********************************************************************
Public Class CardPool

    Private Shared _dataset As DataSet

    Public Structure Card
        Public Name As String
        Public Cost As String
        Public Rules As String
        Public [Type] As String
        Public MultiverseId As String
        Public Color As String
        Public CMC As Integer
    End Structure

    Public Sub New()
        _dataset = New DataSet
        _dataset.ReadXml(Application.StartupPath & "\cardpool\master.xml")
    End Sub

    Public ReadOnly Property AsDataSet As DataSet
        Get
            Return _dataset
        End Get
    End Property

    Public Shared Function GetCard(ByVal MultiverseId As String) As Card
        Dim Result As New Card
        Dim CardRows() As DataRow = _dataset.Tables(0).Select("multiverseid='" & MakeQuerySafe(MultiverseId) & "'")
        Dim CardRow As DataRow = CardRows(0)

        Result.Name = CardRow("name").ToString
        Result.Cost = CardRow("cost").ToString
        Result.Rules = CardRow("rules").ToString
        Result.Type = CardRow("type").ToString
        Result.MultiverseId = CardRow("multiverseid").ToString
        Result.CMC = 0

        Result.CMC = Result.CMC + New Regex("B", RegexOptions.IgnoreCase).Matches(Result.Cost).Count
        Result.CMC = Result.CMC + New Regex("U", RegexOptions.IgnoreCase).Matches(Result.Cost).Count
        Result.CMC = Result.CMC + New Regex("R", RegexOptions.IgnoreCase).Matches(Result.Cost).Count
        Result.CMC = Result.CMC + New Regex("W", RegexOptions.IgnoreCase).Matches(Result.Cost).Count
        Result.CMC = Result.CMC + New Regex("G", RegexOptions.IgnoreCase).Matches(Result.Cost).Count

        Dim tempCostStr As String = Result.Cost
        tempCostStr = tempCostStr.Replace("B", "")
        tempCostStr = tempCostStr.Replace("U", "")
        tempCostStr = tempCostStr.Replace("R", "")
        tempCostStr = tempCostStr.Replace("W", "")
        tempCostStr = tempCostStr.Replace("G", "")
        tempCostStr = tempCostStr.Replace("X", "")

        If Not Trim(tempCostStr) = String.Empty Then
            Result.CMC = Result.CMC + CInt(Trim(tempCostStr))
        End If

        Result.Color = String.Empty
        If Result.Cost.Contains("B") Then
            Result.Color = Result.Color & "B"
        End If
        If Result.Cost.Contains("U") Then
            Result.Color = Result.Color & "U"
        End If
        If Result.Cost.Contains("R") Then
            Result.Color = Result.Color & "R"
        End If
        If Result.Cost.Contains("W") Then
            Result.Color = Result.Color & "W"
        End If
        If Result.Cost.Contains("G") Then
            Result.Color = Result.Color & "G"
        End If

        Return Result
    End Function

    Private Shared Function MakeQuerySafe(ByVal Query As String) As String
        Query = Query.Replace("'", "''")
        Return Query
    End Function

    Public Shared Sub SetCardImage(ByVal ImageControl As PictureBox, ByVal MultiverseId As String)
        If IO.File.Exists(Application.StartupPath & "\cache\" & MultiverseId & ".jpg") Then
            If New IO.FileInfo(Application.StartupPath & "\cache\" & MultiverseId & ".jpg").Length = 0 Then
                Kill(Application.StartupPath & "\cache\" & MultiverseId & ".jpg")
                ImageControl.Image = MCI.My.Resources.Resources.cardback
                ImageControl.ImageLocation = "http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=" & MultiverseId & "&type=card"

                Threading.ThreadPool.QueueUserWorkItem(New Threading.WaitCallback(AddressOf DownloadImageToCache), MultiverseId)
            End If
            ImageControl.Image = Image.FromFile(Application.StartupPath & "\cache\" & MultiverseId & ".jpg")
        Else
            ImageControl.Image = MCI.My.Resources.Resources.cardback
            ImageControl.ImageLocation = "http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=" & MultiverseId & "&type=card"

            Threading.ThreadPool.QueueUserWorkItem(New Threading.WaitCallback(AddressOf DownloadImageToCache), MultiverseId)
        End If
    End Sub

    Public Shared Sub DownloadImageToCache(ByVal MultiverseId As String)
        Using wc As New Net.WebClient()
            Try
                wc.DownloadFile("http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=" & MultiverseId & "&type=card", Application.StartupPath & "\cache\" & MultiverseId & ".jpg")
                If New IO.FileInfo(Application.StartupPath & "\cache\" & MultiverseId & ".jpg").Length = 0 Then
                    Kill(Application.StartupPath & "\cache\" & MultiverseId & ".jpg") 'failed to download
                End If
            Catch
            End Try
        End Using

    End Sub

End Class
